<?php
class SubjectsController extends AppController {

	var $name = 'Subjects';
	var $helpers = array('Ajax','Javascript');
	var $components = array('RequestHandler');
	function admin_index() {
		$this->Subject->recursive = 0;
		$this->set('subjects', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid subject', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('subject', $this->Subject->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Subject->create();
			if ($this->Subject->save($this->data)) {
				$this->Session->setFlash(__('The subject has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The subject could not be saved. Please, try again.', true));
			}
		}
		$categories = $this->Subject->Category->find('list');
		$this->set(compact('categories'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid subject', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Subject->save($this->data)) {
				$this->Session->setFlash(__('The subject has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The subject could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Subject->read(null, $id);
		}
		$categories = $this->Subject->Category->find('list');
		$this->set(compact('categories'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for subject', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Subject->delete($id)) {
			$this->Session->setFlash(__('Subject deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Subject was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	//ThanhBC: hien thi mac dinh khi trang web duoc load
	function display() {
		$this->Subject->recursive = 0;
		$this->set('subjects', $this->paginate());
	}
	function viewexamfee() {
		$this->layout = 'testcenter';
		$this->loadModel('Category');
		$categories = $this->Category->find('list');
		print_r($categories);
		$this->set('categories', $categories);
	}
	function get_subjects_ajax()
	{
		if($this->RequestHandler->isAjax())
		{

			$this->loadModel('Subject');

			$arr = $this->Subject->find('all',
                           array('conditions' =>
                                       array('Subject.category_id' => $this->params['url']['categoryId']),
                                       // array('City.country_id' => $_POST['countryId']),
                                 'recursive' => -1, 'fields'=>array('Subject.id', 'Subject.name', 'Subject.code', 'Subject.cost')));

			// $sql = "Select id, name, code, FORMAT(cost, 0) cost From Subjects t where t.category_id = " . $this->params['url']['categoryId'];
			// $arr = $this->Subject->query($sql);

			$this->set('subjects', $arr);
		}
	}
}
